package cn.iocoder.yudao.module.pay.controller.app.packageorder;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pay.controller.admin.packageorder.vo.PackageOrderRespVO;
import cn.iocoder.yudao.module.pay.controller.app.packageorder.vo.PackageOrderCreateReqVO;
import cn.iocoder.yudao.module.pay.controller.app.packageorder.vo.AppPackageOrderPageReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.packageorder.PackageOrderDO;
import cn.iocoder.yudao.module.pay.service.packageorder.PackageOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "APP（有关笔试） - 套餐订单")
@RestController
@RequestMapping("/pay/package-order")
@Validated
public class AppPackageOrderController {

    @Resource
    private PackageOrderService packageOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建套餐订单")
    public CommonResult<Long> createPackageOrder(@Valid @RequestBody PackageOrderCreateReqVO createReqVO) {
        return success(packageOrderService.createPackageOrder(getLoginUserId(), createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除套餐订单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deletePackageOrder(@RequestParam("id") Long id) {
        packageOrderService.deletePackageOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得套餐订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<PackageOrderRespVO> getPackageOrder(@RequestParam("id") Long id) {
        PackageOrderDO packageOrder = packageOrderService.getPackageOrder(id);
        return success(BeanUtils.toBean(packageOrder, PackageOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得套餐订单分页")
    public CommonResult<PageResult<PackageOrderRespVO>> getPackageOrderPage(@Valid AppPackageOrderPageReqVO pageReqVO) {
        pageReqVO.setUserId(getLoginUserId());
        // 只获取已支付的数据
        pageReqVO.setPayStatus(true);
        PageResult<PackageOrderDO> pageResult = packageOrderService.getPackageOrderPageForApp(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PackageOrderRespVO.class));
    }

}